package utsc.apps.utschedule;

import java.util.ArrayList;
import java.util.List;

import utsc.apps.utschedule.classes.Course;
import utsc.apps.utschedule.classes.Timetable;
import utsc.apps.utschedule.util.database.DatabaseManager;
import android.app.Application;

public class UTSCheduleApp extends Application {
	private List<Timetable> _timetables;
	private Timetable _currentActiveTimetable;
	private ArrayList<Course> _syncedCourses;
	
	public List<Timetable> get_timetables() {
		if (_timetables==null) {
			_timetables=DatabaseManager.getInstance(this).getAllTimetables();
		}
		return _timetables;
	}
	
	public void addTimetable(Timetable timetable) {
		int size=_timetables.size();
		if (size>0) {
			for (int i = 0; i < size; i++) {
				_timetables.get(i).set_status(Timetable.STATUS_INACTIVE);
			}
		}
		setActiveTimetable(timetable);
		_timetables.add(timetable);
		DatabaseManager.getInstance(this).saveTimetables(_timetables);
	}
	
	public void setActiveTimetable(Timetable timetable){
		timetable.set_status(Timetable.STATUS_ACTIVE);
		_currentActiveTimetable=timetable;
		DatabaseManager.getInstance(this).saveTimetable(_currentActiveTimetable);
	}
	
	public Timetable get_currentActiveTimetable() {
		
		if (_currentActiveTimetable==null) {
			_currentActiveTimetable=DatabaseManager.getInstance(this).getCurrentActiveTimetable();
		}
		/*if (get_timetables()!=null && _currentActiveTimetable==null) {
			for (Timetable timetable : _timetables) {
				if (timetable.get_status()==Timetable.STATUS_ACTIVE) {
					_currentActiveTimetable=timetable;
					break;
				}
			}
		}*/
		return _currentActiveTimetable;
	}
	
	public void refreshCurrentTimetable() {
		if (_currentActiveTimetable!=null) {
			DatabaseManager.getInstance(this).refreshTimetable(_currentActiveTimetable);
		}
	}
	
	public void removeTimetable(){
	}

	public ArrayList<Course> get_syncedCourses() {
		return _syncedCourses;
	}

	public void set_syncedCourses(ArrayList<Course> _syncedCourses) {
		this._syncedCourses = _syncedCourses;
	}
}
